package me.luger.project.fof.controller.admin;

import me.luger.core.dao.nutz.entity.page.DataTableRequest;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.core.mvc.interceptor.oplog.OpLog;
import me.luger.project.fof.service.CompanyService;
import me.luger.project.fof.entity.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Company controller
 */
@Controller
@RequestMapping("/private/fof/company")
public class CompanyController extends BaseController {

    private String viewBasePath = "/admin/modules/company/";

    @Autowired
    private CompanyService companyService;

    @RequestMapping(value = "",name = "Company列表页面")
    public ModelAndView index(){
        return btlView(viewBasePath + "index");
    }

    @RequestMapping(value = "data",name = "获取Company列表")
    @ResponseBody
    public Object data(@RequestBody final DataTableRequest dataTableRequest){
        return companyService.data(dataTableRequest);
    }



    @RequestMapping(value = "delete",name = "删除Company接口")
    @ResponseBody
    public Object delete(String[] ids){
        companyService.delete(ids);
        return Result.success("delete success");
    }

    @RequestMapping(value = "/detail/{id}",name = "获取Company详细信息接口")
    public ModelAndView detail(@PathVariable("id") Integer id){
        Company company = companyService.fetchById(id);
        setAttr("company",company);
        return btlView(viewBasePath + "detail");
    }

    @RequestMapping(value = "add",name = "添加Company页面")
    public ModelAndView add(){
        return btlView(viewBasePath + "add");
    }

    @RequestMapping(value = "addDo",name = "添加Company接口")
    @ResponseBody
    @OpLog(name = "添加Company",msg = "添加Company}")
    public Object adddo(Company company){
        try{
            company = companyService.insert(company);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "/edit/{id}",name = "更新Company信息页面")
    public ModelAndView edit(@PathVariable Integer id){
        Company company = companyService.fetchById(id);
        setAttr("company",company);
        return btlView(viewBasePath + "edit");
    }

    @RequestMapping(value = "editDo",name = "更新Company接口")
    @ResponseBody
    public Object editDo(Company company){
        try{
            companyService.updateIgnoreNull(company);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }
}
